#include "script.h"

#include <string>
#include <ctime>

#pragma warning(disable : 4244 4305) // double <-> float conversions

std::string statusText;
DWORD statusTextDrawTicksMax;
bool statusTextGxtEntry;

int red1 = 0, green1 = 0, blue1 = 0, red2 = 0, green2 = 0, blue2 = 0;
bool painting = false;
bool showText = false;
bool switching = false;
std::string color = "green";
std::string part = "primary";

void update_status_text()
{
	if (showText == true)
	{
		UI::SET_TEXT_FONT(0);
		UI::SET_TEXT_SCALE(0.55, 0.55);
		UI::SET_TEXT_COLOUR(255, 255, 255, 255);
		UI::SET_TEXT_WRAP(0.0, 1.0);
		UI::SET_TEXT_CENTRE(1);
		UI::SET_TEXT_DROPSHADOW(0, 0, 0, 0, 0);
		UI::SET_TEXT_EDGE(1, 0, 0, 0, 205);
		if (statusTextGxtEntry)
		{
			UI::_SET_TEXT_ENTRY((char *)statusText.c_str());
		}
		else
		{
			UI::_SET_TEXT_ENTRY("STRING");
			UI::_ADD_TEXT_COMPONENT_STRING((char *)statusText.c_str());
		}
		UI::_DRAW_TEXT(0.5, 0.5);
	}
}

void set_status_text(std::string str, DWORD time = 2500, bool isGxtEntry = false)
{
	statusText = str;
	statusTextDrawTicksMax = time;
	statusTextGxtEntry = isGxtEntry;
}

void update_text()
{
	std::string greenString1 = std::to_string(green1);
	std::string blueString1 = std::to_string(blue1);
	std::string redString1 = std::to_string(red1);

	std::string greenString2 = std::to_string(green2);
	std::string blueString2 = std::to_string(blue2);
	std::string redString2 = std::to_string(red2);

	if (part == "primary")
	{
		if (color == "green" && part == "primary")
		{
			set_status_text("Primary Green: " + greenString1);
		}
		if (color == "blue" && part == "primary")
		{
			set_status_text("Primary Blue: " + blueString1);
		}
		if (color == "red" && part == "primary")
		{
			set_status_text("Primary Red: " + redString1);
		}
	}

	if (part == "secondary")
	{
		if (color == "green" && part == "secondary")
		{
			set_status_text("Secondary Green: " + greenString2);
		}
		if (color == "blue" && part == "secondary")
		{
			set_status_text("Secondary Blue: " + blueString2);
		}
		if (color == "red" && part == "secondary")
		{
			set_status_text("Secondary Red: " + redString2);
		}
	}
}

void update()
{
	Ped playerPed = PLAYER::PLAYER_PED_ID();
	Vehicle veh = PED::GET_VEHICLE_PED_IS_USING(playerPed);
	update_status_text();

	if (GetAsyncKeyState(VK_OEM_MINUS) & 0x8000 && painting == false && (PED::IS_PED_IN_ANY_VEHICLE(playerPed, 0)))
	{
		painting = true;
		showText = true;
	}
	if (!(PED::IS_PED_IN_ANY_VEHICLE(playerPed, 0)))
	{
		painting = false;
		showText = false;
	}
	if (painting == true)
	{
		if (GetAsyncKeyState(VK_NUMPAD7) && part == "primary")
		{
			part = "secondary";
			WAIT(130);
		}
		if (GetAsyncKeyState(VK_NUMPAD7) && part == "secondary")
		{
			part = "primary";
			WAIT(130);
		}

		if (GetAsyncKeyState(VK_NUMPAD1) && part == "primary")
		{
			green2 = green1;
			red2 = red1;
			blue2 = blue1;
		}
		if (GetAsyncKeyState(VK_NUMPAD1) && part == "secondary")
		{
			green1 = green2;
			red1 = red2;
			blue1 = blue2;
		}

		if (switching == true)
		{
			switching = false;
		}

		if (color == "green")
		{
			if (GetAsyncKeyState(VK_NUMPAD8))
			{
				if (part == "primary")
				{
					if (green1 == 255)
					{
						green1 = 0;
					}
					green1 += 1;
				}
				if (part == "secondary")
				{
					if (green2 == 255)
					{
						green2 = 0;
					}
					green2 += 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD2))
			{
				if (part == "primary")
				{
					if (green1 == 0)
					{
						green1 = 255;
					}
					green1 -= 1;
				}
				if (part == "secondary")
				{
					if (green2 == 0)
					{
						green2 = 255;
					}
					green2 -= 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD4))
			{
				color = "red";
				WAIT(130);
			}
			if (GetAsyncKeyState(VK_NUMPAD6))
			{
				color = "blue";
				WAIT(130);
			}
		}
		if (color == "blue")
		{
			if (GetAsyncKeyState(VK_NUMPAD8))
			{
				if (part == "primary")
				{
					if (blue1 == 255)
					{
						blue1 = 0;
					}
					blue1 += 1;
				}
				if (part == "secondary")
				{
					if (blue2 == 255)
					{
						blue2 = 0;
					}
					blue2 += 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD2))
			{
				if (part == "primary")
				{
					if (blue1 == 0)
					{
						blue1 = 255;
					}
					blue1 -= 1;
				}
				if (part == "secondary")
				{
					if (blue2 == 0)
					{
						blue2 = 255;
					}
					blue2 -= 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD4))
			{
				color = "green";
				WAIT(130);
			}
			if (GetAsyncKeyState(VK_NUMPAD6))
			{
				color = "red";
				WAIT(130);
			}
		}
		if (color == "red")
		{
			if (GetAsyncKeyState(VK_NUMPAD8))
			{
				if (part == "primary")
				{
					if (red1 == 255)
					{
						red1 = 0;
					}
					red1 += 1;
				}
				if (part == "secondary")
				{
					if (red2 == 255)
					{
						red2 = 0;
					}
					red2 += 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD2))
			{
				if (part == "primary")
				{
					if (red1 == 0)
					{
						red1 = 255;
					}
					red1 -= 1;
				}
				if (part == "secondary")
				{
					if (red2 == 0)
					{
						red2 = 255;
					}
					red2 -= 1;
				}
			}
			if (GetAsyncKeyState(VK_NUMPAD4))
			{
				color = "blue";
				WAIT(130);
			}
			if (GetAsyncKeyState(VK_NUMPAD6))
			{
				color = "green";
				WAIT(130);
			}
		}
		if (GetAsyncKeyState(VK_RETURN) || GetAsyncKeyState(VK_NUMPAD5))
		{
			VEHICLE::SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(veh, red1 % 255, green1 % 255, blue1 % 255);
			if (VEHICLE::_DOES_VEHICLE_HAVE_SECONDARY_COLOUR(veh))
				VEHICLE::SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(veh, red2 % 255, green2 % 255, blue2 % 255);

			showText = false;
			painting = false;
		}
		if (GetAsyncKeyState(VK_DELETE))
		{
			showText = false;
			painting = false;
		}
	}
}

void main()
{
	while (true)
	{
		update();
		update_text();
		WAIT(0);
	}
}

void ScriptMain()
{
	srand(GetTickCount());
	main();
}